package net.etfbl.projekat.verko.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import net.etfbl.projekat.verko.dto.Reziser;

public class ReziserDAO {
	
	public static ArrayList<Reziser> getAllReziser(DataManager dm)
	{
		Connection conn=dm.checkOutConnection();
		ArrayList<Reziser> rezultat= new ArrayList<Reziser>();
		int id_reziser;
		String ime;
		final String upit="select * from tbl_reziser";
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs= ps.executeQuery();
					try
					{
						while(rs.next())
						{
							id_reziser=rs.getInt(1);
							ime=rs.getString(2);
							rezultat.add(new Reziser(id_reziser,ime));
							}
					}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u ReziserDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
	
	public static void addReziser(DataManager dm, Reziser reziser)
	{
		final String upit="insert into tbl_reziser values(null, ?)";
		
		Connection conn= dm.checkOutConnection();
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ps.setString(1, reziser.getIme());
					ps.execute();
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u ReziserDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
	}
	
	public static void updateReziser(DataManager dm, Reziser reziser)
	{
		final String upit="update tbl_reziser set ime=? where id_reziser = "+reziser.getId_reziser();
		
		Connection conn= dm.checkOutConnection();
		
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ps.setInt(1, reziser.getId_reziser());
					ps.setString(1, reziser.getIme());
					ps.execute();
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska ReziserDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
	}
	
	public static ArrayList<Reziser> filterByIme(DataManager dm, Reziser reziser)
	{
		final String upit="select * from tbl_reziser where ime="+reziser.getIme();
		Connection conn = dm.checkOutConnection();
		ArrayList<Reziser> rezultat=new ArrayList<Reziser>();
		if(conn!=null)
		{
			try
			{
				PreparedStatement ps= conn.prepareStatement(upit);
				try
				{
					ResultSet rs= ps.executeQuery();
					try
					{
						while(rs.next())
						{
							rezultat.add(new Reziser(rs.getInt(1),rs.getString(2)));
						}
						}
					finally
					{
						rs.close();
					}
				}
				finally
				{
					ps.close();
				}
			}
			catch(SQLException e)
			{
				System.out.println("Greska u ReziserDAO: "+e.getMessage());
			}
			finally
			{
				dm.checkInConnection(conn);
			}
		}
		return rezultat;
	}
}